package epoch.game.core.timer;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * 服务器定时任务管理器
 * @author Administrator
 *
 */
public class ServerTimerManager {
	
	ScheduledExecutorService scheduledThreadPool = null;
	
	private ServerTimerManager(){
		scheduledThreadPool = Executors.newScheduledThreadPool(1);
	}
	
	private static ServerTimerManager instance;
	public static ServerTimerManager getInstance(){
		if(instance == null){
			instance = new ServerTimerManager();
		}
		return instance;
	}
	
	/**
	 * 执行一次
	 * @param task
	 * @param delay
	 */
	public ScheduledFuture<?> addTask(Runnable task, long delay){
		return scheduledThreadPool.schedule(task, delay, TimeUnit.MILLISECONDS);
	}
	
	/**
	 * 循环执行，固定时间间隔（以执行结束计时）
	 * @param task
	 * @param delay
	 * @param period
	 */
	public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay, long period){
		return scheduledThreadPool.scheduleWithFixedDelay(task, delay, period, TimeUnit.MILLISECONDS);
	}
	
	
	/**
	 * 循环执行，固定时间间隔（以开始执行计时）
	 * @param task
	 * @param delay
	 * @param period
	 */
	public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long delay, long period){
		return scheduledThreadPool.scheduleAtFixedRate(task, delay, period, TimeUnit.MILLISECONDS);
	}
	
}
